package viqiang.ye.mallpuny.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import viqiang.ye.mallpuny.common.api.CommonResult;
import viqiang.ye.mallpuny.dto.UmsAdminLoginParam;
import viqiang.ye.mallpuny.mbg.model.UmsAdmin;
import viqiang.ye.mallpuny.mbg.model.UmsPermission;
import viqiang.ye.mallpuny.service.UmsAdminService;

import java.util.List;
import java.util.Map;

@Api(tags = "UmsAdminController", description = "后台用户管理")
@Controller
@RequestMapping("/admin/")
public class UmsAdminController {

    @Autowired
    private UmsAdminService adminService;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;


    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "register", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<UmsAdmin> register(@RequestBody UmsAdmin umsAdmin, BindingResult result) {
        return adminService.register(umsAdmin);
    }


    @ApiOperation(value = "登录以后返回token")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestBody UmsAdminLoginParam loginParam, BindingResult bindResult) {
        Map tokenMap = adminService.login(loginParam.getUsername(), loginParam.getPassword());
        if (CollectionUtils.isEmpty(tokenMap)) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        return CommonResult.success(tokenMap);
    }

    @ApiOperation("获取用户所有权限（包括+-权限）")
    @RequestMapping(value = "/permission/{adminId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsPermission>> getPermissionList(@PathVariable Long adminId) {
        List<UmsPermission> umsPermissions = adminService.getPermissionList(adminId);
        return CommonResult.success(umsPermissions);
    }

}
